package com.sinodata.bsm.center.engine.event.worker;

import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.center.cache.EventTypeCache;
import com.sinodata.bsm.center.cache.NewEventCache;
import com.sinodata.bsm.center.engine.event.EventWorker;
import com.sinodata.bsm.center.service.event.EventService;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.constants.EventConstants;
import com.sinodata.bsm.common.vo.Event;

/**
 * 
 * <p>
 * Description: 事件处理工作流：事件恢复
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-29 AM 10:20:14    liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ResumeEventWorker implements EventWorker {

    private static Logger logger = Logger.getLogger(ResumeEventWorker.class);

    private static NewEventCache newEventCache = SpringContextHolder.getBean(NewEventCache.class);

    private static EventTypeCache eventTypeCache = SpringContextHolder.getBean(EventTypeCache.class);

    private static EventService eventService = SpringContextHolder.getBean(EventService.class);

    public ResumeEventWorker() {

    }

    @Override
    public void execute(Event event) {
        if (!(eventTypeCache.get(event.getEventTypeId()).getRecoverable() == EventConstants.TYPE_RECOVERABLE)) {
            return;
        }
        List<Event> events = newEventCache.getNewEvents(event.getResId(), event.getEventTypeId(), event.getObjectId());
        for (int i = events.size() - 1; i >= 0; i--) {
            if (events.get(i).getId().longValue() == event.getId().longValue()) {
                events.remove(i);
            }
        }
        //规则清除事件
        if (event.getCatalog() == EventConstants.CATALOG_RULE_CLEAR) {
            if (event.getObjectId() != null) {// 对象ID 不为空 则清除所有事件
                events = newEventCache.getNewEvents(event.getResId(), event.getEventTypeId(), null);
                for (int i = events.size() - 1; i >= 0; i--) {
                    if (events.get(i).getId().longValue() == event.getId().longValue()) {
                        events.remove(i);
                    }
                }
                //end
            }
            for (int i = 0; i < events.size(); i++) {
                String msg = "【因事件规则被修改或删除,系统自动清除该规则产生的事件】";
                eventService.confirm(events.get(i).getId(), EventConstants.CONFIRM_TYPE_EVENT, "" + event.getId(), EventConstants.REMARKER_SYSTEM, msg, false);
            }
            return;
        }

        if (event.getCatalog() == EventConstants.CATALOG_FAULTEVENT) {//同级别事件，默认压制(过滤)
            for (int i = 0; i < events.size(); i++) {
                if (events.get(i).getLevel().intValue() == event.getLevel().intValue()) {
                    if (event.getObjectId() == null || event.getObjectId().equals(events.get(i).getObjectId())) {
                        try {
                            eventService.confirm(event.getId(), EventConstants.CONFIRM_TYPE_FILTER, "" + events.get(i).getId(), EventConstants.REMARKER_SYSTEM, "事件已存在", true);
                        } catch (Exception e) {
                            logger.error("failed to confirm event ", e);
                        }
                        return;
                    }
                }
            }
        }
        if (event.getCatalog() == EventConstants.CATALOG_FAULTEVENT) {
            for (int i = 0; i < events.size(); i++) {
                if (events.get(i).getLevel() <= event.getLevel()) {
                    if (events.get(i).getId().longValue() != event.getId().longValue()) {
                        if (event.getObjectId() == null || event.getObjectId().equals(events.get(i).getObjectId())) {
                            String confirmMsg = "【故障升级】【系统自动清除低级别事件】" + event.getMsg();
                            eventService.confirm(events.get(i).getId(), EventConstants.CONFIRM_TYPE_EVENT, "" + event.getId(), EventConstants.REMARKER_SYSTEM, confirmMsg, false);
                        }
                    }
                }
            }
        } else { //非故障事件（故障恢复事件）
            //TODO 考虑健康度事件
            for (int i = 0; i < events.size(); i++) {
                if (events.get(i).getLevel() >= event.getLevel()) {
                    if (event.getObjectId() == null || event.getObjectId().equals(events.get(i).getObjectId())) {
                        String msg = "【故障恢复】【系统自动清除事件】" + event.getMsg();
                        eventService.confirm(events.get(i).getId(), EventConstants.CONFIRM_TYPE_EVENT, "" + event.getId(), EventConstants.REMARKER_SYSTEM, msg, false);
                    }
                }
            }
        }
    }

}